<?php

class UserController extends Controller {

    public function init() {
        $this->layout = "main";
    }

    public function actionRegister() {
        $data['thanhpho'] = array();
        $data['errors'] = array();
        $data['quocgia'] = Quocgia::model()->findAll();
        $data['loaihinhhoatdong'] = Loaihinhhoatdong::model()->findAll();
        $data['data'] = Yii::app()->request->getParam('data');
        if ($data['data']) {
            $nhatuyendung = new Nhatuyendung;
            $nhatuyendung->setAttributes($data['data'], false);
            $nhatuyendung->matkhau_confirm = $data['data']['matkhau_confirm'];
            $nhatuyendung->security_code = $data['data']['security_code'];
            if ($nhatuyendung->save()) {
                Yii::app()->session['nhatuyendung'] = array(
                    'tendangnhap' => $data['data']['tendangnhap'],
                    'id' => $nhatuyendung->id,
                );
            } else {
                $data['thanhpho'] = Thanhpho::model()->findAll("id_quocgia = :nid", array("nid" => $data['data']['id_quocgia']));
                $data['errors'] = $nhatuyendung->getErrors();
            }
        }
        if (isset(Yii::app()->session['nhatuyendung'])) {
            $this->redirect(Yii::app()->baseUrl . "/nhatuyendung");
        }
        $this->render('register', $data);
    }

    public function actionLogin() {
        $data['errors'] = array();
        $data['data'] = Yii::app()->request->getParam('data');
        if ($data['data']) {
            $user = Nhatuyendung::model()->findAll("tendangnhap = :username AND matkhau = :password", array("username" => $data['data']['tendangnhap'], "password" => md5($data['data']['matkhau'])));
            if (!empty($user)) {
                Yii::app()->session['nhatuyendung'] = array(
                    'tendangnhap' => $user[0]['tendangnhap'],
                    'id' => $user[0]['id'],
                );
            } else {
                $data['errors'] = array('matkhau' => array("message" => "Sai tên nhập hoặc mật khẩu !"));
            }
        }
        if (isset(Yii::app()->session['nhatuyendung'])) {
            $this->redirect(Yii::app()->baseUrl . "/nhatuyendung");
        }
        $this->render('login', $data);
    }

    public function actionhr_central() {
        $this->render('hr_central');
    }
    public function actionhr_central_quanlyhosoungvien() {
        $this->render('hr_central_quanlyhosoungvien');
    }
    public function actionkiemungvien() {
        $this->render('kiemungvien');
    }
    public function actiontaothongbaoungvien() {
        $this->render('taothongbaoungvien');
    }
public function actioncamnangtuyendung() {
        $this->render('camnangtuyendung');
    }
    public function actionLogout() {
        $this->layout = false;
        unset(Yii::app()->session['nhatuyendung']);
        $this->redirect(Yii::app()->baseUrl . "/nhatuyendung");
        Yii::app()->end();
    }

    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
                'foreColor' => 0x348017,
                'testLimit' => 1,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

}